# -*- coding: utf-8 -*-
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from apiv10.models import Supply, Join, Customized, HomeForm


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')


class SupplySerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    name = serializers.CharField(allow_blank=False)
    phone = serializers.CharField(allow_blank=False)
    email = serializers.CharField(allow_blank=True)
    type = serializers.CharField(allow_blank=False)
    company_name = serializers.CharField(allow_blank=False)
    local = serializers.CharField(allow_blank=True)
    summary = serializers.CharField(allow_blank=False)

    def create(self, validated_data):
        return Supply.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.phone = validated_data.get('phone', instance.phone)
        instance.email = validated_data.get('email', instance.email)
        instance.type = validated_data.get('type', instance.type)
        instance.company_name = validated_data.get('company_name', instance.company_name)
        instance.local = validated_data.get('local', instance.local)
        instance.summary = validated_data.get('summary', instance.summary)
        instance.save()
        return instance


# class JoinSerializer(serializers.Serializer):
#     id = serializers.IntegerField(read_only=True)
#     name = serializers.CharField(allow_blank=False)
#     phone = serializers.CharField(allow_blank=False)
#     sex = serializers.CharField(allow_blank=True)
#     email = serializers.CharField(allow_blank=True)
#     company_name = serializers.CharField(allow_blank=False)
#     local = serializers.CharField(allow_blank=True)
#     education = serializers.CharField(allow_blank=False)
#     school_name = serializers.CharField(allow_blank=True)
#
#     def create(self, validated_data):
#         return Supply.objects.create(**validated_data)
#
#     def update(self, instance, validated_data):
#         instance.name = validated_data.get('name', instance.name)
#         instance.phone = validated_data.get('phone', instance.phone)
#         instance.sex = validated_data.get('email', instance.sex)
#         instance.email = validated_data.get('email', instance.email)
#         instance.company_name = validated_data.get('company_name', instance.company_name)
#         instance.local = validated_data.get('local', instance.local)
#         instance.education = validated_data.get('local', instance.education)
#         instance.school = validated_data.get('local', instance.school)
#         instance.save()
#         return instance


class JoinSerializer(serializers.ModelSerializer):
    class Meta:
        model = Join
        fields = ('id', 'name', 'phone', 'sex', 'email', 'city', 'company_name', 'local', 'education', 'school_name')


class CustomizedSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customized
        fields = ('id', 'name', 'phone', 'email', 'duty', 'students_num', 'school_name', 'school_type', 'info')


class HomeFormSerializer(serializers.ModelSerializer):
    class Meta:
        model = HomeForm
        fields = ('id', 'name', 'company_name', 'phone', 'email')
